import axios from 'axios';
import base from './config';
// const baseURL = 'http://localhost:8080/api';
const baseURL= `${base}/api`

export const listPostsService = async (listData) => {
    try {
        const response = await axios.post(`${baseURL}/post/list`, listData);
        return response.data;
    } catch (error) {
        console.error('Error fetching posts:', error);
        throw error;
    }
};

export const createPostService = async (postData) => {
    try {
        const response = await axios.post(`${baseURL}/post/create`, postData, {
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        });
        return response.data;
    } catch (error) {
        console.error('Error creating post:', error);
        throw error;
    }
}

export const fetchPostDetailService = async (postId, page, pageSize) => {
    const params = {
        postId: postId,
        page: page || 1,
        pageSize: pageSize || 10
    }
    try {
        const response = await axios.get(`${baseURL}/post/detail`, {
            params: params
        });
        if (response.status !== 200) {
            throw new Error(`Error fetching post detail: ${response.statusText}`);
        }
       return response.data;
    } catch (error) {
        console.error('Error fetching post detail:', error);
        throw error;
    }
}

export const fetchPostLikeService = async (postId) => {
    try {
        const response = await axios.get(`${baseURL}/reaction/like/status/${postId}`);
        return response.data;
    } catch (error) {
        console.error('Error fetching post likes:', error);
        throw error;
    }
}

export const createCommentService = async (commentData) => {
    try {
        const response = await axios.post(`${baseURL}/comment/create`, commentData , {
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        });
        return response.data;
    } catch (error) {
        console.error('Error creating comment:', error);
        throw error;
    }
}

export const deleteCommentService = async (commentId) => {
    try {
        const response = await axios.delete(`${baseURL}/comment/delete/${commentId}`);
        return response.data;
    } catch (error) {
        console.error('Error deleting comment:', error);
        throw error;
    }
}

export const deletePostService = async (postId) => {
    try {
        const response = await axios.delete(`${baseURL}/post/delete/${postId}`);
        return response.data;
    } catch (error) {
        console.error('Error deleting post:', error);
        throw error;
    }
}

export const likePostService = async (postId) => {
    try {
        const response = await axios.post(`${baseURL}/reaction/like`, null, {
            params: { postId: postId }
        });
        return response.data;
    } catch (error) {
        console.error('Error liking post:', error);
        throw error;
    }
}

export const cancelLikePostService = async (postId) => {
    try {
        const response = await axios.post(`${baseURL}/reaction/cancelLike`, null, {
            params: { postId: postId }
        });
        return response.data;
    } catch (error) {
        console.error('Error canceling like on post:', error);
        throw error;
    }
}

export const listUserPostsService = async (userId, page = 1, pageSize = 200) => {
    try {
        const response = await axios.get(`${baseURL}/post/sbPost`, {
            params: { userId: userId, page: page, pageSize: pageSize }
        });
        return response.data;
    } catch (error) {
        console.error('Error fetching user posts:', error);
        throw error;
    }
}

export const createReportService = async (reportData) => {
    try {
        const response = await axios.post(`${baseURL}/report`, reportData);
        return response.data;
    } catch (error) {
        console.error('Error creating report:', error);
        throw error;
    }
}

 export const fetchPinnedPostService = async (forumId) => {
    try {
        const response = await axios.get(`${baseURL}/post/pinnedList`, {
            params: { forumId: forumId }
        });
        return response.data;
    } catch (error) {
        console.error('Error fetching pinned posts:', error);
        throw error;
    }
}

export const updatePostService = async (postData) => {
    try {
        const response = await axios.post(`${baseURL}/post/update`, postData);
        return response.data;
    } catch (error) {
        console.error('Error updating post:', error);
        throw error;
    }
}

export const lockPostService = async (postId) => {
    try {
        const response = await axios.post(`${baseURL}/post/lock`, null, {
            params: { postId: postId }
        });
        return response.data;
    } catch (error) {
        console.error('Error locking post:', error);
        throw error;
    }
}